package com.ken.base_library.widget

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.res.TypedArray
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View.OnClickListener
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.ImageView
import com.ken.base_library.R
import com.ken.base_library.base.IBaseView

/**
 * @author by ken
 * Blog:
 * Date:2020/5/9
 * Description:
 */
class MySearchView(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs),
    IBaseView {
    lateinit var etSearch: EditText
    lateinit var ivSearch: ImageView
    private val mContext: Context = context
    private val mAttrs: AttributeSet? = attrs

    init {
        initView(mContext)
        initAttrs(mContext, mAttrs)
    }

    @SuppressLint("InflateParams")
    override fun initView(context: Context) {
        val view = LayoutInflater.from(context).inflate(R.layout.view_my_search, null)
        etSearch = view.findViewById(R.id.et_search)
        ivSearch = view.findViewById(R.id.iv_search)
        addView(view)
    }

    override fun initAttrs(context: Context, attrs: AttributeSet?) {
        val typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.MySearchView)
        val hint_search_text =
            typedArray.getString(R.styleable.MySearchView_hint_search_text)
        val search_text = typedArray.getString(R.styleable.MySearchView_search_text)
        //输入框的默认的提示内容的字体颜色
        //输入框的默认的提示内容的字体颜色
        val defaultHintTextColor = context.resources.getColor(R.color.code_cf)
        val defaultTextColor = context.resources.getColor(R.color.black)
        val hintTextColor = typedArray.getColor(
            R.styleable.MySearchView_hint_search_text_color,
            defaultHintTextColor
        )
        val textColor =
            typedArray.getColor(R.styleable.MySearchView_search_text_color, defaultTextColor)

        val search_src = typedArray.getResourceId(R.styleable.MySearchView_search_scr, 0)
        val searchTextSize =
            typedArray.getInt(R.styleable.MySearchView_search_text_size, 18).toFloat()

        etSearch.hint = hint_search_text
        etSearch.setText(search_text)
        etSearch.setHintTextColor(hintTextColor)
        etSearch.setTextColor(textColor)
        etSearch.textSize = searchTextSize
        ivSearch.setImageResource(if (search_src == 0) R.mipmap.search else search_src)
        //        回收
        typedArray.recycle()
    }


    /**
     * 设置输入框的提示字符
     */
    fun setHintSearchText(arg: String?) {
        etSearch.hint = arg
    }

    fun setSearchText(arg: String?) {
        etSearch.setText(arg)
    }

    fun getSearchText(): String? {
        return etSearch.text.toString()
    }


    fun setSearchImageViewOnClick(listener: OnSearchImageViewOnClickListener) {
        ivSearch.setOnClickListener(OnClickListener {
            listener.onClick()
        })
    }


    /**
     *
     * */
    fun setSearchOnClickListener(listener: OnSearchOnClickListener) {
        etSearch.setOnKeyListener { view, i, keyEvent ->
            //                是否是回车键
            if (i == KeyEvent.KEYCODE_ENTER && keyEvent.action == KeyEvent.ACTION_DOWN) {
                //                    隐藏键盘
                (mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
                    .hideSoftInputFromWindow(
                        (mContext as Activity).currentFocus?.windowToken,
                        InputMethodManager.HIDE_NOT_ALWAYS
                    )
                //                    软键盘搜索按键的搜索事件
                listener.onSearchClick()
            }
            false
        }
    }

    fun addSearchViewTextChange(afterTextChange: OnSearchAfterTextChange) {
        etSearch.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(
                charSequence: CharSequence,
                i: Int,
                i1: Int,
                i2: Int
            ) {
                afterTextChange.beforeTextChanged(charSequence, i, i1, i2)
            }

            override fun onTextChanged(
                charSequence: CharSequence,
                i: Int,
                i1: Int,
                i2: Int
            ) {
                afterTextChange.onTextChanged(charSequence, i, i1, i2)
            }

            override fun afterTextChanged(editable: Editable) {
                afterTextChange.onAfterTextChanged(editable)
            }
        })
    }

    interface OnSearchImageViewOnClickListener {
        /**
         * 搜索框右侧图片的点击事件
         */
        fun onClick()
    }

    interface OnSearchOnClickListener {
        /**
         * 搜索事件
         */
        fun onSearchClick()
    }

    /**
     * 输入框事件
     * */
    interface OnSearchAfterTextChange {
        fun beforeTextChanged(
            charSequence: CharSequence?,
            i: Int,
            i1: Int,
            i2: Int
        )

        fun onTextChanged(
            charSequence: CharSequence?,
            i: Int,
            i1: Int,
            i2: Int
        )

        fun onAfterTextChanged(editable: Editable?)
    }


}